#ifndef __MODBUS_PROTOCOL_TABLE_H__
#define __MODBUS_PROTOCOL_TABLE_H__

#include <stdint.h>

typedef struct _PROTOCOL_MODBUS_POINT{
    int64_t  id;          //测点ID
    int64_t  protocolID;  //协议ID
    int64_t  modelID;     //模型ID
    int64_t  modelPointID;//模型测点ID
    char name[32];        //测点名称
    int  dataType;        //数据类型
    int  regAddr;         //寄存器地址
    int  regNum;          //寄存器数量
    int  funcCode;        //功能码
    int  pos;             //数据开始位置， 默认0
    int  bits;            //数据位宽， 默认1
    int  scale;           //数据比例因子， 默认1
    char formula[128];    //数据计算公式
    int  decimal;         //小数位数， 默认0
    char endian[8];       //数据字节序， 默认"AB"/"ABCD"
    struct _PROTOCOL_MODBUS_POINT *prev;
    struct _PROTOCOL_MODBUS_POINT *next;
}T_PROTOCOL_MODBUS_POINT;

typedef struct _PROTOCOL_MODBUS_POINT_LIST{
    int count;
    T_PROTOCOL_MODBUS_POINT *head;
    T_PROTOCOL_MODBUS_POINT *tail;
}T_PROTOCOL_MODBUS_POINT_LIST;

typedef struct _PROTOCOL_MODBUS_CTRL_POINT{
    int64_t  id;          //控制点ID
    int64_t  protocolID;  //协议ID
    int64_t  pointID;     //测点ID
    int64_t  modelID;     //模型ID
    int64_t  modelCtrlPointID;//模型控制点ID
    char name[32];        //指令名称
    int  dataType;        //数据类型
    int  regAddr;         //寄存器地址
    int  regNum;          //寄存器数量
    int  funcCode;        //功能码                             
    int  pos;             //数据开始位置， 默认0
    int  bits;            //数据位宽， 非线圈/离散量时默认0， 线圈/离散量时默认1
    int  scale;           //数据比例因子， 默认1
    char formula[128];    //数据计算公式
    int  decimal;         //小数位数， 默认0
    char endian[8];       //数据字节序， 默认"AB"/"ABCD"
    char value[128];      //默认值
    struct _PROTOCOL_MODBUS_CTRL_POINT *prev;
    struct _PROTOCOL_MODBUS_CTRL_POINT *next;
}T_PROTOCOL_MODBUS_CTRL_POINT;

typedef struct _PROTOCOL_MODBUS_CTRL_POINT_LIST{
    int count;
    T_PROTOCOL_MODBUS_CTRL_POINT *head;
    T_PROTOCOL_MODBUS_CTRL_POINT *tail;
}T_PROTOCOL_MODBUS_CTRL_POINT_LIST;

typedef struct _PROTOCOL_MODBUS_REQUEST{
    int64_t  id;          //请求ID
    int64_t  protocolID;  //协议ID
    char name[32];        //请求名称
    int  dataType;        //数据类型
    int  regAddr;    //寄存器地址
    int  regNum;          //寄存器数量或位数
    int  funcCode;        //功能码                             
    struct _PROTOCOL_MODBUS_REQUEST *prev;
    struct _PROTOCOL_MODBUS_REQUEST *next;
}T_PROTOCOL_MODBUS_REQUEST;

typedef struct _PROTOCOL_MODBUS_REQUEST_LIST{
    int count;
    T_PROTOCOL_MODBUS_REQUEST *head;
    T_PROTOCOL_MODBUS_REQUEST *tail;
}T_PROTOCOL_MODBUS_REQUEST_LIST;


extern void dbModbusInit();
extern int createModbusPointTable();
extern int insertModbusPoint(T_PROTOCOL_MODBUS_POINT* point);
extern int replaceModbusPoint(T_PROTOCOL_MODBUS_POINT* point);
extern int deleteModbusPoint(T_PROTOCOL_MODBUS_POINT* point);
extern int selectModbusPoint(const char* sql, T_PROTOCOL_MODBUS_POINT_LIST* pointList);
/*===============================================================*/
/* modbus 指令表 */
extern int createModbusCtrlPointTable();
extern int insertModbusCtrlPoint(T_PROTOCOL_MODBUS_CTRL_POINT* ctrlPoint);
extern int replaceModbusCtrlPoint(T_PROTOCOL_MODBUS_CTRL_POINT* ctrlPoint);
extern int deleteModbusCtrlPoint(T_PROTOCOL_MODBUS_CTRL_POINT* ctrlPoint);
extern int selectModbusCtrlPoint(const char* sql, T_PROTOCOL_MODBUS_CTRL_POINT_LIST* cmdList);
/*===============================================================*/
/* modbus 请求表 */
extern int createModbusRequestTable();
extern int insertModbusRequest(T_PROTOCOL_MODBUS_REQUEST* request);
extern int replaceModbusRequest(T_PROTOCOL_MODBUS_REQUEST* request);
extern int deleteModbusRequest(T_PROTOCOL_MODBUS_REQUEST* request);
extern int selectModbusRequest(const char* sql, T_PROTOCOL_MODBUS_REQUEST_LIST* reqList);



#endif /* __MODBUS_PROTOCOL_TABLE_H__ */